﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace MainClient
{
    public partial class frmLogin : Form
    {
        private string strCheckcode;

        public frmLogin()
        {
            InitializeComponent();
            cb_duty.SelectedIndex = 0;
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            string usr = txt_usr.Text.Trim();
            string pwd = txt_pwd.Text.Trim();
            if (usr=="" || usr.Equals(""))
            {
                MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pwd == "" || pwd.Equals(""))
            {
                MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txt_checkcode.Text.ToLower() != strCheckcode.ToLower())
            {
                MessageBox.Show("验证码错误, 请重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                setCheckcode();
                return;
            }

            try
            {
                // Check username and password.
                DataAccess.UserDAO userDAO = new DataAccess.UserDAO();
                if (userDAO.isUserExist(usr, pwd))
                {
                    //Config login user information
                    LoginUser.setLoginUser(usr, userDAO.getPermission(usr), this.cb_duty.Text);

                    MainWindow main = new MainWindow(this.txt_usr.Text.Trim(), "Undefined");
                    main.Show();
                    this.Visible = false;
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                    setCheckcode();
                }
            }
            catch
            {
                string strSql = "连接数据库失败！请运行数据库配置程序先进行数据库配置，然后重新连接！";
                MessageBox.Show(strSql, "错误",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            setCheckcode();
        }

        private void setCheckcode() 
        {
            // Clear value of strCheckcode.
            this.strCheckcode = "";
            txt_checkcode.Text = "";

            // Create check code and display it in RichTextBox
            int width = 80;
            int height = 30;

            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            // Create Random.
            Random random = new Random();

            // Clear image background color and fill it with random color.
            g.Clear(getRandColor(200, 250));

            // Draw random lines.
            for (int i = 0; i < 5; i++)
            {

                int x = random.Next(width - 1);
                int y = random.Next(height - 1);
                int x1 = random.Next(width - 1);
                int y1 = random.Next(height - 1);
                g.DrawLine(new Pen(getRandColor(50, 200)), x1, y1, x, y);
            }
            Font mFont = new Font("Arial", 12, FontStyle.Bold);
            LinearGradientBrush brush = new LinearGradientBrush(
                new Rectangle(0, 0, width, height),
                Color.Blue, Color.DarkRed, 1.2f,
                true);

            String sRand = "";
            // Create random checkcode string.
            int itmp = 0;
            for (int i = 0; i < 4; i++)
            {
                if (random.Next(2) == 1)
                {
                    itmp = random.Next(26) + 65;	// Create letter between A~Z.
                }
                else
                {
                    itmp = random.Next(10) + 48;	// Create digtal between 0~9.
                }
                char ctmp = (char)itmp;
                sRand = ctmp.ToString();
                this.strCheckcode += sRand;

                g.RotateTransform((float)((random.NextDouble()-0.5)*15));
                g.DrawString(sRand, mFont, brush, 19 * i , 2);
                g.ResetTransform();
            }
            // Draw fore points.
            for (int j = 0; j < 150; j++)
            {
                int x = random.Next(width - 2);
                int y = random.Next(height - 2);
                bmp.SetPixel(x, y, getRandColor(100, 250));
            }
            // Draw image border lines.
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, width - 1, height - 1);
            this.pic_checkcode.Image = bmp;
        }

        private Color getRandColor(int s, int e)
        {
            Random random = new Random();
            if (s > 255) s = 255;
            if (e > 255) e = 255;
            int r = s + random.Next(e - s);
            int g = s + random.Next(e - s);
            int b = s + random.Next(e - s);
            return Color.FromArgb(r, g, b);
        }

        private void pic_checkcode_Click(object sender, EventArgs e)
        {
            setCheckcode();
        }

        private void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btn_login_Click(sender, e);
            }
        }
    }
}
